package cn.stylefeng.guns.core.util;

import cn.stylefeng.roses.core.util.ToolUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiUtil {
    /**
     * 获取分页信息
     * @return
     */
    public static Map<String,Integer> getPageInfo(HttpServletRequest request){
        String pageNumber = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if(ToolUtil.isEmpty(pageSize)){
            pageSize = String.valueOf(10);
        }
        int start = 0;
        if (ToolUtil.isNotEmpty(pageNumber)) {
            int pageNum = Integer.parseInt(pageNumber);
            if(pageNum>0){
                start = (pageNum-1)* Integer.parseInt(pageSize);
            }
        }
        Map<String,Integer> map = new HashMap<>();
        map.put("start", start);
        map.put("page_size", Integer.parseInt(pageSize));
        return map;
    }

    /**
     * 获取基础路径
     * @param request
     * @return
     */
    public static String getBasePath(HttpServletRequest request){
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
        return basePath;
    }


}
